package m.jclecai.com.activity.follow;

import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.okhttp.Request;

import java.io.IOException;
import java.util.HashMap;

import butterknife.BindView;
import m.jclecai.com.R;
import m.jclecai.com.activity.LotteryPayActivity;
import m.jclecai.com.activity.SuccessActivity;
import m.jclecai.com.base.BaseActivity;
import m.jclecai.com.bean.ProofBean;
import m.jclecai.com.constant.Constant_Result;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.data.Lecai_SDK;
import m.jclecai.com.listener.Callback;
import m.jclecai.com.utils.BaseUtils;
import m.jclecai.com.utils.PreferenceUtil;
import m.jclecai.com.utils.http.FastOk;
import m.jclecai.com.utils.http.HttpManager;

/**
 * Created by Dinosa on 2017/11/30.
 * 跟单确认的activity;
 */

public class FollowOrderConfirmActivity extends BaseActivity {


    @BindView(R.id.iv_lottery_icon)
    ImageView mIvLotteryIcon;

    @BindView(R.id.tv_lottery_name)
    TextView mTvLotteryName;

    @BindView(R.id.tv_plan_money)
    TextView mTvPlanMoney;

    @BindView(R.id.tv_need_money)
    TextView mTvNeedMoney;

    @BindView(R.id.tv_actual_money)
    TextView mTvActualMoney;

    @BindView(R.id.tv_user_total_money)
    TextView mTvUserTotalMoney;

    @BindView(R.id.tv_confirm)
    TextView mTvConfirm;
    private String mPlanId;
    private String mMuil;
    private String mMoney;
    private String mType;
    private String hbid = "";//Yes	string	红包ID
    private String hbmoney = "0";//	Yes	string	红包金额


    @Override
    protected int getContentView() {
        return R.layout.activity_follow_order_confirm;
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("确认提交");
        leftOptionText("");
    }

    @Override
    protected void initData() {
        super.initData();

        checkLogin(new Callback(){

            @Override
            public void callbackField() {
                super.callbackField();
                finish();
            }

            @Override
            public void callbackSuccess() {
                super.callbackSuccess();
                //这里我们做初始化操作；

                fillDataToView();

            }
        });
    }

    public static Intent getIntent(Context context, String id, String muli,String lotteryName,String totalMoney){
        Intent intent = new Intent(context, FollowOrderConfirmActivity.class);
        intent.putExtra("id",id);
        intent.putExtra("muil",muli);
        intent.putExtra("type",lotteryName);
        intent.putExtra("money",totalMoney);
        return intent;
    }

    /**
     * 将数据填充到view里面去；
     */
    private void fillDataToView() {

        mPlanId = getIntent().getStringExtra("id");
        mMuil = getIntent().getStringExtra("muil");
        mMoney = getIntent().getStringExtra("money");
        mType = getIntent().getStringExtra("type");

        mTvLotteryName.setText(mType);


        String waitPay="<html>待支付: <font color='red'>￥"+ mMoney;


        mTvActualMoney.setText(Html.fromHtml(waitPay));

        mTvPlanMoney.setText("￥"+mMoney+"元");

        String needMoney="<html>金额<font color='red'>￥"+ mMoney +"<font/> 优惠金额<font color='red'>￥0元</font></html>";

        mTvNeedMoney.setText(Html.fromHtml(needMoney));

        mTvUserTotalMoney.setText(PreferenceUtil.readUser(this).getAllmoney()+"元");

    }

    @Override
    protected void initListener() {
        super.initListener();
        mTvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Lecai_SDK.from = "Follow";
                Intent intent = new Intent(FollowOrderConfirmActivity.this, LotteryPayActivity.class);
                intent.putExtra(PAY_MULTIPLE, mMuil);
                //得到倍数和code和金额
                intent.putExtra(PAY_CODES, "");
                intent.putExtra(PAY_MONEY,mMoney);
                intent.putExtra(PAY_VOTENUMS, "1");
                intent.putExtra(PAY_TYPE, "dlt");
                intent.putExtra(PAY_NAME, "自购");
                 intent.putExtra(PAY_TERM, "");
                startActivityForResult(intent, Constant_Result.CLOSEPAGE);
            }
        });
    }

    /**
     * 这里的这个操作就是确定定制跟单；
     */
    public void requestConfirm(String user,String pwd,String id, String multi,String channe){

        showDialog();

        HashMap<String, String> paramsMap = new HashMap<>();

        paramsMap.put("user",user);
        paramsMap.put("password",pwd);
        paramsMap.put("id",id);
        paramsMap.put("multiple",multi);
        paramsMap.put("channel",channe);
        paramsMap.put(PAY_HBID, hbid);//Yes	string	红包ID
        paramsMap.put(PAY_HBMONEY, hbmoney);//	Yes	string	红包金额

        HttpManager.postAsyn(Constant_Url.ADD_PROJECT_FOLLOW, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {

                DisPlay("网络连接异常!!!");
                dissDialog();

            }

            @Override
            public void onResponse(String response) {

                dissDialog();

                if(!TextUtils.isEmpty(response)){

                    ProofBean proofBean = FastOk.get(response, ProofBean.class);

                    if(proofBean!=null){

                        if( "0".equals(proofBean.getStatus())){

                            //这里我们要跳转到成功的页面；
                            Intent intent = new Intent(FollowOrderConfirmActivity.this, SuccessActivity.class);
                            intent.putExtra("tradeType", 0);
                            startActivity(intent);
                            setResult(RESULT_OK);

                        }else{
                            DisPlay(proofBean.getMessage());
                        }

                    }else{
                        DisPlay("网络连接异常!!!");
                    }

                }else{
                    DisPlay("网络连接异常!!!");
                }

            }
        },paramsMap);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constant_Result.CLOSEPAGE && resultCode ==RESULT_OK){
            hbid = data.getStringExtra("hbid");
            hbmoney = data.getStringExtra("hbmoney");
            Log.e("==hb==",hbid+ " -- "+hbmoney);
            String userName = PreferenceUtil.getUserName(FollowOrderConfirmActivity.this);
            String password = PreferenceUtil.getPassword(FollowOrderConfirmActivity.this);
            requestConfirm(userName,password,mPlanId,mMuil,"android");
        }
    }
}
